package com.kaishengit.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.kaishengit.pojo.User;
import com.kaishengit.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String email;
	private int count;
	private User user;
	private String oldpwd;
	private String newpwd;
	private String confirmpwd;
	
	private UserService userService;
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String checkEmail() {
	    count = userService.emailCount(email);
	    try {
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(count);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
	    return null;
	}
	
	public String editUser() {
	    Map<String, Object> session = ServletActionContext.getContext().getSession();
	    User user2 = (User) session.get("user");

	    if (user2.getPassword().equals(user.getPassword())) {
	        user.setGroup(userService.findUserGroupById(user.getGroup().getId()));
	        user.setEnable(true);
	        userService.save(user);
	        session.put("user", user);
	        return SUCCESS;
	    } else {
	        return ERROR;
	    }
	}
	
	public String toPassword() {
	    return SUCCESS;
	}
	
	public String editPassword() {
	    Map<String, Object> session = ServletActionContext.getContext().getSession();
        User user = userService.findById(((User) session.get("user")).getId());
        if (oldpwd.equals(user.getPassword())) {
            if (newpwd.equals(confirmpwd)) {
                user.setPassword(newpwd);
                userService.save(user);
                session.put("user", user);
                return SUCCESS; 
            } else {
                return ERROR;
            }
        } else {
            return ERROR;
        }
	}
	
	public String loginOut() {
	    Map<String, Object> session = ServletActionContext.getContext().getSession();
	    session.put("user", null);
	    return SUCCESS;
	}

	//get set
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getOldpwd() {
        return oldpwd;
    }

    public void setOldpwd(String oldpwd) {
        this.oldpwd = oldpwd;
    }

    public String getNewpwd() {
        return newpwd;
    }

    public void setNewpwd(String newpwd) {
        this.newpwd = newpwd;
    }

    public String getConfirmpwd() {
        return confirmpwd;
    }

    public void setConfirmpwd(String confirmpwd) {
        this.confirmpwd = confirmpwd;
    }
}
